package com.zjt.videoplayer;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zjt.itemvideoplayer.ItemVideoPlayer;
import com.zjt.itemvideoplayer.ItemVideoPlayerManager;
import com.zjt.videoplayer.adpter.VideoPlayerListAdapter;
import com.zjt.videoplayer.bean.Video;

import java.util.ArrayList;
import java.util.List;

public class VideoPlayerListActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private VideoPlayerListAdapter videoPlayerListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_videoplayer_list);
        initRecyclerView();
    }

    private void initRecyclerView(){
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setHasFixedSize(true);
        videoPlayerListAdapter = new VideoPlayerListAdapter(this, getVideoList());
        mRecyclerView.setAdapter(videoPlayerListAdapter);
        mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                ItemVideoPlayer videoPlayer = (ItemVideoPlayer) view.findViewById(R.id.item_video_view);
                if (videoPlayer != null) {
                    videoPlayer.release();
                }
            }
        });
    }

    public List<Video> getVideoList() {
        List<Video> videoList = new ArrayList<>();
        videoList.add(new Video("办公室小野开番外了，居然在办公室开澡堂！老板还点赞？",
                "http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-17_17-30-43.jpg",
                "http://meitu-iflow.video.ums.uc.cn/video/wemedia/dde96bb239634e2f9191a39b9b7309ae/05d906349bb611284cc9cdafbbeaae70-137539252-4-2.mp4?auth_key=1511421863-0-0-d376d864679970540c0b917f59ce7b30"));

        videoList.add(new Video("CCTV",
                "http://pubimage.360doc.com/cntv.jpg",
                "http://v.cctv.com/flash/mp4video6/TMS/2011/01/05/cf752b1c12ce452b3040cab2f90bc265_h264818000nero_aac32-1.mp4"));

        videoList.add(new Video("小野在办公室用丝袜做茶叶蛋 边上班边看《外科风云》",
                "http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-10_10-09-58.jpg",
                "http://bm-vod-in.oss-cn-shanghai.aliyuncs.com/record/baimei/1496217772959-21075866/1496217772959-210758661496223500000.m3u8"));

        for (int i = 0; i < 2; i++) {
            videoList.add(new Video("可乐爆米花，嘭嘭嘭......收花的人说要把我娶回家",
                    "http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/04/2017-04-21_16-37-16.jpg",
                    "http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/04/2017-04-21_16-41-07.mp4"));
        }

        videoList.add(new Video("CCTV",
                "http://pubimage.360doc.com/cntv.jpg",
                "http://v.cctv.com/flash/mp4video6/TMS/2011/01/05/cf752b1c12ce452b3040cab2f90bc265_h264818000nero_aac32-1.mp4"));

        videoList.add(new Video("办公室小野开番外了，居然在办公室开澡堂！老板还点赞？",
                "http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-17_17-30-43.jpg",
                "http://flv2.bn.netease.com/videolib3/1604/28/fVobI0704/SD/movie_index.m3u8"));

        return videoList;
    }

    @Override
    public void onBackPressed() {
        if (ItemVideoPlayerManager.instance().onBackPressd()) {
            return;
        }
        super.onBackPressed();
    }
}
